/*
 * watcher.cpp
 *
 *  Created on: 10-jul-2009
 *      Author: HPK
 */

#include "watcher.h"

Watcher::Watcher(Point pos,Ground* ground)
	:	_ground(ground){
	_pos = pos;
}

Watcher::~Watcher(){

}

void Watcher::limitWatcher(){
	Point wcenter = this->_pos;
	Cube gcube = this->_ground->getDimensions();

	if(wcenter.x1 < gcube.minX()){
		this->_pos += Point(gcube.minX()-wcenter.x1,0,0);
	}
	else if(wcenter.x1 > gcube.maxX()){
		this->_pos += Point(gcube.maxX()-wcenter.x1,0,0);
	}

	if(wcenter.x2 < gcube.minY()){
		this->_pos += Point(0,gcube.minY()-wcenter.x2,0);
	}else if(wcenter.x2 > gcube.maxY()){
		this->_pos += Point(0,gcube.maxY()-wcenter.x2,0);
	}

	calculateWatcherZ();
}

void Watcher::calculateWatcherZ(){
	_pos.x3 = _ground->calculateZ(_pos.x1,_pos.x2) + 2.0f;
}

void Watcher::moveWatcherTo(float x,float y){
	moveWatcher(x-_pos.x1,y-_pos.x2);
}

void Watcher::moveWatcher(float dx,float dy){
	_pos.x1 += dx;
	_pos.x2 += dy;

	limitWatcher();
	calculateWatcherZ();
}
